package com.yanqu.road.server.cmd.activity.springdinner;

import com.yanqu.road.entity.activity.springdinner.BallData;
import com.yanqu.road.entity.activity.springdinner.ComposeData;
import com.yanqu.road.pb.activity.SpringDinnerProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.springdinner.SpringDinnerModule;
import com.yanqu.road.server.pb.SpringDinnerPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Cmd(code = Protocol.S_SPRINGDINNER_GAME_COMPOSE, desc = "请求进入合成大西瓜游戏界面")
public class SpringDinnerGameComposeCmd extends AbstractCommand {

    private int activityId;
    private int type;
    private double x;//水果下落的位置
    private double y;//水果下落的位置
    private Map<Integer, BallData> ballMap;
    private Map<Integer, ComposeData> composeMap;

    SpringDinnerProto.SpringDinnerComposeRespMsg.Builder respMsg;



    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        SpringDinnerProto.SpringDinnerComposeReqMsg reqMsg = SpringDinnerProto.SpringDinnerComposeReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        type = reqMsg.getType();
        if(reqMsg.getPositionCount() != 2){
            ret = GameErrorCode.E_SPRINGDINNER_GAME_BALL_ERR;
            return true;
        }
        x = Double.parseDouble(reqMsg.getPosition(0));
        y = Double.parseDouble(reqMsg.getPosition(1));
        ballMap = new ConcurrentHashMap<>();
        ret = SpringDinnerPb.parseReqBallData(ballMap,reqMsg.getFruitsList());
        if(ret != 0)return true;
        composeMap = new ConcurrentHashMap<>();
        ret = SpringDinnerPb.parseReqComposeData(composeMap,reqMsg.getComposeDataList());
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if(ret != 0){
            return;
        }
        ret = player.getModule(SpringDinnerModule.class).checkCommon(activityId,type);
        if(ret != 0){
            return;
        }
        respMsg = player.getModule(SpringDinnerModule.class).composeBall(type,ballMap,composeMap,x,y);

    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            respMsg = SpringDinnerProto.SpringDinnerComposeRespMsg.newBuilder();
            respMsg.setRet(ret);
        }
        player.sendPacket(Protocol.U_SPRINGDINNER_GAME_COMPOSE, respMsg);
    }
}
